package com.tairan.chapter.security.web;

import org.springframework.util.StringUtils;

import java.io.Serializable;

public class ResultEntity<T> implements Serializable {

    private int status;

    private String message;

    private T data;

    private ResultEntity(int status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static <E> ResultEntity<E> build(ResultStatus status, String message, E data) {
        String msg = StringUtils.isEmpty(message) ? status.getMessage() : message;
        return new ResultEntity(status.getStatus(), msg, data);
    }

    public static <E> ResultEntity<E> build(ResultStatus status) {
        return build(status, null, null);
    }

    public static <E> ResultEntity<E> buildSuccess() {
        return build(ResultStatus.SUCCESS, null, null);
    }

    public static <E> ResultEntity<E> buildSuccess(E data) {
        return build(ResultStatus.SUCCESS, null, data);
    }

    public static <E> ResultEntity<E> buildFailed(E data) {
        return build(ResultStatus.FAILED, null, data);
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultEntity{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
